package com.example.dqhl.recycleviewdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;


import com.example.dqhl.recycleviewdemo.R;

/**
 * @author guanluocang
 * @data 2020/2/26
 * @description
 */
public class HIndicator extends View {

    private Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private RectF mBgRect = new RectF();

    private Float mRadius = 0f;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private RectF mRect = new RectF();

    private int viewWidth = 0;
    private int mBgColor = Color.parseColor("#e5e5e5");
    private int mIndicatorColor = Color.parseColor("#ff4646");
    float mRatio = 0.5f;       //长度比例

    public void setRatio(float ratio) {
        this.mRatio = ratio;
        invalidate();
    }

    Float mProgress = 0f;    //滑动进度比例

    public void setProgress(Float progress) {
        this.mProgress = progress;
        invalidate();
    }


    public HIndicator(Context context) {
        this(context, null);
    }

    public HIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HIndicator);
        mBgColor = typedArray.getColor(R.styleable.HIndicator_hi_bgColor, mBgColor);
        mIndicatorColor = typedArray.getColor(R.styleable.HIndicator_hi_indicatorColor, mIndicatorColor);
        typedArray.recycle();

        mBgPaint.setColor(mBgColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mIndicatorColor);
        mPaint.setStyle(Paint.Style.FILL);
    }


    public void setBgColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }


    public void setIndicatorColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        mBgRect.set(0f, 0f, w * 1f, h * 1f);
        mRadius = h / 2f;
    }

    public void bindRecy(final RecyclerView recyclerViews) {
        recyclerViews.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                float offsetX = recyclerView.computeHorizontalScrollOffset();
                float range = recyclerView.computeHorizontalScrollRange();
                float extend = recyclerView.computeHorizontalScrollExtent();
                float progress = offsetX * 1.0f / (range - extend);
                //设置滚动距离所占比例
                setProgress(progress);
            }
        });
        recyclerViews.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                float range = recyclerViews.computeHorizontalScrollRange();
                float extend = recyclerViews.computeHorizontalScrollExtent();
                float ratio = extend * 1f / range;
                //设置指示器所占的长度比例
                setRatio(ratio);
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        canvas.drawRoundRect(mBgRect, mRadius, mRadius, mBgPaint);
        //计算指示器的长度和位置
        float leftOffSet = viewWidth * (1f - mRatio) * mProgress;
        float left = mBgRect.left + leftOffSet;
        float right = left + viewWidth * mRatio;
        mRect.set(left, mBgRect.top, right, mBgRect.bottom);
        //绘制指示器
        canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);

    }
}
